package com.sxy.controller;

import com.sxy.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author 宋鑫宇
 * @version 1.0
 * 使用Rest风格
 */
//定义controller
//@Controller
@RestController
//每个功能模块按照书写规定，定义路径前缀
@RequestMapping("/users")
//@ResponseBody
public class UserController {

    //设置当前操作的访问路径
    //使用Restful风格设置响应方法
//    @RequestMapping(method = RequestMethod.POST)
    //设置当前操作的返回值类型
//    @ResponseBody
    @PostMapping
    public String save() {
        System.out.println("user save....");
        return "{'module','springMVC'}";
    }

    //请求接收参数,不区分post和get请求方式,如果前端传递参数名和后端不相同，需要在参数前添加@RequestParam注解来让系统识别
    //Restful风格编写删除
//    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
//    @ResponseBody
    @DeleteMapping("/{id}")
    public String delete(@PathVariable int id) {
        System.out.println("Restful delete..." + id);
        return "{'id=" + id + "'}";
    }

    //Restful风格编写修改
//    @RequestMapping( method = RequestMethod.PUT)
//    @ResponseBody
    @PutMapping
    public String update(@RequestBody User user) {
        System.out.println("Restful update..." + user);
        return "{'user update'}";
    }

    //Restful风格编写根据ID查询
//    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
//    @ResponseBody
    @GetMapping("/{id}")
    public String selectById(@PathVariable int id) {
        System.out.println("Restful selectById..." + id);
        return "{'user selectById :"+id+"'}";
    }

    //Restful风格编写查询所有
//    @RequestMapping(method = RequestMethod.GET)
//    @ResponseBody
    @GetMapping
    public String selectAll() {
        System.out.println("Restful selectAll...");
        return "{'user selectAll'}";
    }

    //使用引用类型传参,可以嵌套但传参时需要加嵌套名.参数名
    @RequestMapping("/pojoContainPojoParam")
    @ResponseBody
    public String pojoContainPojoParam(User user) {
        System.out.println(user);
        return "{'module','pojo Contain Pojo Param'}";
    }

    //使用数组进行传参
    @RequestMapping("/arrayParam")
    @ResponseBody
    public String arrayParam(String[] likes) {
        System.out.println(Arrays.toString(likes));
        return "{'module','array Param'}";
    }

    //使用集合进行传参,需要在集合参数前声明@RequestParam
    @RequestMapping("/listParam")
    @ResponseBody
    public String listParam(@RequestParam List<String> likes) {
        System.out.println(likes);
        return "{'module','list Param'}";
    }

    //使用json格式传递参数,首先在pom.xml中导入json坐标,其次在springMvc配置中开启@EnableWebMvc
    //用集合接收json数据,在参数前加上@RequestBody注解
    @RequestMapping("/jsonListParam")
    @ResponseBody
    public String jsonListParam(@RequestBody List<String> likes) {
        System.out.println(likes);
        return "{'module','json list Param'}";
    }

    //使用json给对象传参
    @RequestMapping("/pojoJsonParam")
    @ResponseBody
    public String pojoJsonParam(@RequestBody User user) {
        System.out.println(user);
        return "{'module','pojo Contain Pojo Param'}";
    }

    //json使用集合接收多个对象参数
    @RequestMapping("/jsonPojoListParam")
    @ResponseBody
    public String jsonPojoListParam(@RequestBody List<User> users) {
        System.out.println(users);
        return "{'module','json Pojo List Param'}";
    }

    //日期型数据转换
    @RequestMapping("/dateParam")
    @ResponseBody
    public String dateParam(@DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date date) {
        System.out.println("data=" + date);
        return "{'module','data Param'}";
    }

    //响应页面/跳转页面
    @RequestMapping("/toJumpPage")
    public String toJumpPage() {
        System.out.println("跳转页面~");
        return "../index.jsp";
    }

    //响应pojo对象（json数据）,因springmvc默认返回的是字符串,需要设置响应体
    @RequestMapping("/toJsonPojo")
    @ResponseBody
    public User toJsonPojo() {
        System.out.println("响应json数据");
        User user = new User();
        user.setName("SXY");
        user.setAge(21);
        return user;
    }

    //响应pojo集合
    @RequestMapping("/toJsonList")
    @ResponseBody
    public List<User> toJsonList() {
        System.out.println("响应json集合数据");
        ArrayList<User> users = new ArrayList<>();
        User user = new User();
        user.setName("SXY");
        user.setAge(21);

        User user1 = new User();
        user.setName("SXY");
        user.setAge(21);

        users.add(user);
        users.add(user1);
        return users;
    }
}
